package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.EquipmentCalibration;
import com.mxpio.erp.common.equipment.entity.EquipmentCalibrationStatusRecord;
import com.mxpio.erp.common.equipment.entity.EquipmentEqpBasics;
import com.mxpio.erp.common.equipment.service.EquipmentCalibrationService;
import com.mxpio.erp.common.equipment.service.EquipmentCalibrationStatusRecordService;
import com.mxpio.erp.common.equipment.service.EquipmentEqpBasicsService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.security.entity.User;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 设备检定
 * @Author: qiubo
 * @Date: 2023-12-18
 * @Version: v1.0
 */

@Service
@Slf4j
public class EquipmentCalibrationServiceImpl extends BaseServiceImpl<EquipmentCalibration> implements EquipmentCalibrationService {


    @Autowired
    private EquipmentCalibrationStatusRecordService statusRecordService;

    @Autowired
    private EquipmentEqpBasicsService basicsService;


    @Override
    @Transactional
    public Result<EquipmentCalibration> updateCalStatus(EquipmentCalibration equipmentCalibration, EquipmentEnums.Calibration improgress) {
        equipmentCalibration.setExecutionStatus(improgress.getCode());
        this.update(equipmentCalibration);
        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.LSSUED.getCode());
        // 用户
        equipmentCalibration.setInspectDutyUser(equipmentCalibration.getInspectDutyUser());
        // 时间
        equipmentCalibration.setActualStartDate(equipmentCalibration.getActualStartDate());
        // 原因
        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.LSSUED.getCode());
        insertStatusRecord(equipmentCalibration.getCalCode(), equipmentCalibration.getExecutionStatus(), equipmentCalibration.getExecutionStatus());
        return Result.OK();
    }

    @Override
    @Transactional
    public Result<EquipmentCalibration> updateTime(EquipmentCalibration equipmentCalibration, EquipmentEnums.Calibration improgress) {
        equipmentCalibration.setExecutionStatus(improgress.getCode());
        equipmentCalibration.setActualStartDate(new Date());
        this.update(equipmentCalibration);
        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.IMPROGRESS.getCode());
        // 用户
        equipmentCalibration.setInspectDutyUser(equipmentCalibration.getInspectDutyUser());
        // 时间
        equipmentCalibration.setActualStartDate(equipmentCalibration.getActualStartDate());
        // 原因
        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.IMPROGRESS.getCode());
        insertStatusRecord(equipmentCalibration.getCalCode(), equipmentCalibration.getExecutionStatus(), equipmentCalibration.getExecutionStatus());
        return Result.OK();
    }

//    @Override
//    @Transactional
//    public Result<EquipmentCalibration> completeTime(EquipmentCalibration equipmentCalibration, EquipmentEnums.Calibration improgress) {
//        equipmentCalibration.setExecutionStatus(improgress.getCode());
//        equipmentCalibration.setActualEndDate(equipmentCalibration.getActualEndDate());
//        this.update(equipmentCalibration);
//        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.COMPLETED.getCode());
//        // 用户
//        equipmentCalibration.setInspectDutyUser(equipmentCalibration.getInspectDutyUser());
//        // 时间
//        equipmentCalibration.setActualStartDate(equipmentCalibration.getActualStartDate());
//        // 原因
//        equipmentCalibration.setExecutionStatus(EquipmentEnums.Calibration.COMPLETED.getCode());
//        insertStatusRecord(equipmentCalibration.getCalCode(), equipmentCalibration.getExecutionStatus(), equipmentCalibration.getExecutionStatus());
//        return Result.OK();
//    }

    @Override
    @Transactional
    public void listExecute(EquipmentCalibration equipmentCalibration) {
        EquipmentCalibration selectPm = this.getById(EquipmentCalibration.class, equipmentCalibration.getCalCode());
        // 状态
        selectPm.setExecutionStatus(EquipmentEnums.Calibration.PLAN.getCode());
        // 用户
        selectPm.setInspectDutyUser(equipmentCalibration.getInspectDutyUser());
        // 时间
        selectPm.setActualStartDate(equipmentCalibration.getActualStartDate());
        // 原因
        selectPm.setExecutionStatus(EquipmentEnums.Calibration.PLAN.getCode());
        this.update(selectPm);
        insertStatusRecord(equipmentCalibration.getCalCode(), equipmentCalibration.getExecutionStatus(), equipmentCalibration.getExecutionStatus());
    }

    /**
     *  完成检定任务
     * @param equipmentCalibration
     * @return
     */
    @Override
    @Transactional
    public Result<?> finish(EquipmentCalibration equipmentCalibration) {
        EquipmentCalibration bean = this.getById(EquipmentCalibration.class,equipmentCalibration.getCalCode());
        if (bean == null){
            return Result.error("找不到该条数据");
        }
        String targetId = equipmentCalibration.getTargetId();//去得到 targetID的值
        EquipmentEqpBasics equipmentEqpBasics = basicsService.getById(EquipmentEqpBasics.class, targetId);//找到设备台账关联的字段和表
        if (equipmentCalibration.getExecutionStatus().equals(EquipmentEnums.Calibration.IMPROGRESS.getCode())){
            bean.setTestUnit(equipmentCalibration.getTestUnit());
            bean.setTestResult(equipmentCalibration.getTestResult());
            bean.setFile(equipmentCalibration.getFile());
            bean.setExecutionStatus(EquipmentEnums.Calibration.COMPLETED.getCode());
            bean.setActualEndDate(equipmentCalibration.getActualEndDate());
            this.update(bean);//更新信息

            //存操作记录
            insertStatusRecord(equipmentCalibration.getCalCode(), bean.getExecutionStatus(), bean.getExecutionStatus());
            //对设备台账中的上次检定日期进行更新
            basicsService.updateLastTime(equipmentEqpBasics,equipmentCalibration.getCalCode());
            return Result.OK("完成检定单");
        }else {
            return Result.error("该项目不在执行中，无法完成");
        }

    }

    /**
     * 批量完成
     * @param equipmentCalibration
     * @return
     */
    @Override
    @Transactional
    public Result<?> batchCompletion(String calCodes,EquipmentCalibration equipmentCalibration) {
        String[] calCode = calCodes.split(",");
        for (String key : calCode){
            //先进行判断，数据是否存在
            EquipmentCalibration bean = this.getById(EquipmentCalibration.class,key);
            if (bean == null){
                return Result.error("找不到该条数据");
            }
            String targetId = bean.getTargetId();//去得到 targetID的值
            EquipmentEqpBasics equipmentEqpBasics = basicsService.getById(EquipmentEqpBasics.class, targetId);//找到设备台账关联的字段和表
            if (bean.getExecutionStatus().equals(EquipmentEnums.Calibration.IMPROGRESS.getCode())){
                bean.setTestUnit(equipmentCalibration.getTestUnit());
                bean.setTestResult(equipmentCalibration.getTestResult());
                bean.setFile(equipmentCalibration.getFile());
                bean.setExecutionStatus(EquipmentEnums.Calibration.COMPLETED.getCode());
                bean.setActualEndDate(equipmentCalibration.getActualEndDate());
                this.update(bean);//更新信息

                //存操作记录
                insertStatusRecord(key, bean.getExecutionStatus(), bean.getExecutionStatus());
                //对设备台账中的上次检定日期进行更新
                basicsService.updateLastTime(equipmentEqpBasics,key);
            }else {
                return Result.error("该项目不在执行中，无法完成");
            }
        }
        return Result.OK("完成检定单");
    }

    private void insertStatusRecord(String calCode, String operate, String reason) {
        EquipmentCalibrationStatusRecord statusRecord = new EquipmentCalibrationStatusRecord();//对状态记录进行赋值
        //设置calCode
        statusRecord.setCalCode(calCode);
        //设置操作节点(状态)
        statusRecord.setOperate(operate);
        //设置操作人
        User loginUser = SecurityUtils.getLoginUser();
        statusRecord.setOperator(loginUser.getNickname() + "/" + loginUser.getUsername());
        //设置操作时间
        statusRecord.setOperateTime(new Date());
        //设置原因
        statusRecord.setReason(reason);
        //保存
        statusRecordService.save(statusRecord);
    }
}
